package com.jlu.test;


import com.jlu.dao.CustomerDao;
import com.jlu.domain.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Jpqltest {

  @Autowired
  private CustomerDao customerDao;

  //  根据姓名查询
  @Test
  public void findJpql() {
    Customer customer = customerDao.findJpql("will");
    System.out.println(customer);
  }

  //  根据姓名和id查询
  @Test
  public void findCustNameCustId(){
    Customer customer = customerDao.findCustNameCustId(2l,"will");
    System.out.println(customer);
  }

  /*
  更新
  springDataJpa中使用jpql完成更新/册除操作
     需要手动添加事务的支持
     默认会执行结束之后，回滚事务
  Rollback :设置是否自动回滚
     false | true

  */
  @Test
  @Transactional
  @Rollback(value = false)
  public void update(){
    customerDao.updateByJpql("kane",4l);
  }

  @Test
  public void findByCustName(){
    System.out.println(customerDao.findByCustName("will"));
  }

  @Test
  public void findByCustNameLike(){
    List<Customer> customers = customerDao.findByCustNameLike("w%");
    for (Customer customer : customers) {
      System.out.println(customer);
    }
  }

  @Test
  public void findByCustNameAndID(){
    System.out.println(customerDao.findByCustNameAndId("will", 2l));
  }
}


